package com.hamizhong.iflowers.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.IUserDao;
import com.hamizhong.iflowers.db.entity.User;
import com.hamizhong.iflowers.service.IUserService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MD5Utils;
import com.hamizhong.iflowers.utils.MyConstants;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.AdminVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.UserVO;

public class UserServiceImpl extends MyBaseService implements IUserService {
	private IUserDao iUserDao = null;

	public IUserDao getiUserDao() {
		return iUserDao;
	}
	public void setiUserDao(IUserDao iUserDao) {
		this.iUserDao = iUserDao;
	}
	
	public IOPResultDto doRegistUser(UserVO userVO) {
		IOPResultDto<UserVO> dto = new IOPResultDto<UserVO>();
		User user = new User();
		BeanUtils.copyProperties(userVO, user);
		user.setUserPassword(MD5Utils.generateForPassword(userVO.getUserPassword()));
		iUserDao.add(user);
		dto.setSuccess(true);
		return dto;
	}
	
	public IOPResultDto findByEmail(String userEmail) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userEmail", userEmail);
		List<User> userList = new ArrayList<User>();
		userList = iUserDao.findByExample(paramMap);
		UserVO userVO = null;
		List<UserVO> userVOList = new ArrayList<UserVO>();
		for (User iuser : userList) {
			userVO = new UserVO();
			BeanUtils.copyProperties(iuser, userVO);
			userVO.setUserPassword("");
			userVOList.add(userVO);
		}
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setUserVOList(userVOList);
		dto.addToResultList(iObjectVO);
		dto.setSuccess(true);
		return dto;
	}
	public IOPResultDto findByPhone(String userPhone) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userPhone", userPhone);
		List<User> userList = new ArrayList<User>();
		userList = iUserDao.findByExample(paramMap);
		UserVO userVO = null;
		List<UserVO> userVOList = new ArrayList<UserVO>();
		for (User iuser : userList) {
			userVO = new UserVO();
			BeanUtils.copyProperties(iuser, userVO);
			userVO.setUserPassword("");
			userVOList.add(userVO);
		}
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setUserVOList(userVOList);
		dto.addToResultList(iObjectVO);
		dto.setSuccess(true);
		return dto;
	}
	public IOPResultDto doLoginUser(UserVO userVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		List<User> userList = new ArrayList<User>();
		HashMap<String, Object> param = new HashMap<String, Object>();
		if(null != userVO.getUserEmail() && !"".equals(userVO.getUserEmail())) {
			param.put("userEmail", userVO.getUserEmail());
		}else if(null != userVO.getUserPhone() && !"".equals(userVO.getUserPhone())) {
			param.put("userPhone", userVO.getUserPhone());
		}
		userList = iUserDao.findByExample(param);
		if(userList.size() > 0) {
			if(userList.get(0).getUserPassword().equals(MD5Utils.generateForPassword(userVO.getUserPassword()))) {
				UserVO userVOLogin = new UserVO();
				BeanUtils.copyProperties(userList.get(0), userVOLogin);
				userVOLogin.setUserPassword("");
				IObjectVO iObjectVO = new IObjectVO();
				List<UserVO> userVOList = new ArrayList<UserVO>();
				userVOList.add(userVOLogin);
				iObjectVO.setUserVOList(userVOList);
				dto.addToResultList(iObjectVO);
				dto.setSuccess(true);
			}else {
				dto.setErrorMSG("对不起，账号或密码不正确！");
				dto.setSuccess(false);
			}
		}else {
			dto.setErrorMSG("对不起，账号或密码不正确！");
			dto.setSuccess(false);
		}
		return dto;
	}
	
	public IOPResultDto<IObjectVO> findByCondition(String searchCondition, Integer pageNum, int pageSize) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		List<User> userList = iUserDao.findByCondtion(searchCondition, pageNum, pageSize);
		int totalUesrCnt = iUserDao.findTotalUserCntByCondition(searchCondition);
		List<UserVO> userVOList = new ArrayList<UserVO>();
		UserVO userVO = null;
		if(userList == null) {
			dto.setSuccess(false);
			dto.setErrorMSG("当前搜索条件下无记录！");
			return dto;
		}
		for (User user : userList) {
			userVO = new UserVO();
			BeanUtils.copyProperties(user, userVO);
			userVO.setUserPassword("");
			userVOList.add(userVO);
		}
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setUserVOList(userVOList);
		iObjectVO.setTotalUserCnt(totalUesrCnt);
		dto.addToResultList(iObjectVO);
		dto.setSuccess(true);
		return dto;
	}
	
	public IOPResultDto<IObjectVO> findByUserId(Integer userId) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			User user = iUserDao.findById(userId);
			List<UserVO> userVOList = new ArrayList<UserVO>();
			UserVO userVO = null;
			if (user != null) {
				userVO = new UserVO();
				BeanUtils.copyProperties(user, userVO);
				userVO.setUserPassword("");
				
				userVOList.add(userVO);
				
				IObjectVO iObjectVO = new IObjectVO();
				iObjectVO.setUserVOList(userVOList);
				dto.addToResultList(iObjectVO);
				dto.setSuccess(true);
			}else {
				dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_QUERY_USER);
				dto.setSuccess(false);
			}
			return dto;
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_QUERY_USER);
			dto.setSuccess(false);
			return dto;
		}
	}
	
	public IOPResultDto<IObjectVO> updateUser(UserVO userVO, Integer opId) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			User user = iUserDao.findById(userVO.getUserId());
			
			if (user != null) {
				
				//验证用户名
				if(!user.getUserName().equals(userVO.getUserName())) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("userName", userVO.getUserName());
					List<User> userList = iUserDao.findByExample(map);
					if(userList != null && userList.size() > 0) {
						dto.setErrorMSG("对不起，用户名["+userVO.getUserName()+"]已被注册！");
						dto.setSuccess(false);
						return dto;
					}
				}
				//验证电子邮箱
				if(!user.getUserEmail().equals(userVO.getUserEmail())) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("userEmail", userVO.getUserEmail());
					List<User> userList = iUserDao.findByExample(map);
					if(userList != null && userList.size() > 0) {
						dto.setErrorMSG("对不起，电子邮箱["+userVO.getUserEmail()+"]已被注册！");
						dto.setSuccess(false);
						return dto;
					}
				}
				//验证手机号
				if(!user.getUserPhone().equals(userVO.getUserPhone())) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("userPhone", userVO.getUserPhone());
					List<User> userList = iUserDao.findByExample(map);
					if(userList != null && userList.size() > 0) {
						dto.setErrorMSG("对不起，联系方式["+userVO.getUserPhone()+"]已被注册！");
						dto.setSuccess(false);
						return dto;
					}
				}
				
				user.setUserName(userVO.getUserName());
				user.setUserEmail(userVO.getUserEmail());
				user.setUserPhone(userVO.getUserPhone());
				user.setUpdateDate(new Date());
				user.setUpdateUser(opId);
				iUserDao.update(user);
				dto.setSuccess(true);
			}else {
				dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
				dto.setSuccess(false);
			}
			return dto;
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
			dto.setSuccess(false);
			return dto;
		}
	}
	
	public IOPResultDto<IObjectVO> updateUserPassword(UserVO userVO, String newUserPassword, Integer opId) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			User user = iUserDao.findById(userVO.getUserId());
			
			if (user != null) {
				user.setUserPassword(MD5Utils.generateForPassword(newUserPassword));
				user.setUpdateDate(new Date());
				user.setUpdateUser(opId);
				iUserDao.update(user);
				dto.setSuccess(true);
			}else {
				dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
				dto.setSuccess(false);
			}
			return dto;
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
			dto.setSuccess(false);
			return dto;
		}
	}
	public IOPResultDto<IObjectVO> checkUserPassword(Integer userId, String oldUserPassword) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			User user = iUserDao.findById(userId);
			
			if (user != null) {
				if(!user.getUserPassword().equals(MD5Utils.generateForPassword(oldUserPassword))) {
					dto.setErrorMSG(MyErrorMsgConstants.ERROR_USER_OLD_PASSWORD_FOR_UPDATE);
					dto.setSuccess(false);
				}else {
					dto.setSuccess(true);
				}
			}else {
				dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_QUERY_USER);
				dto.setSuccess(false);
			}
			return dto;
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
			dto.setSuccess(false);
			return dto;
		}
	}
	
}
